<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片区域 -->
      <el-card>
        <!-- 标签页 -->
        <el-tabs>
          <!-- 左 -->
          <el-tab-pane label="角色管理">
            <!-- 添加角色按钮 -->
            <el-row style="height:60px">
              <el-button type="primary" icon="el-icon-plus" size="small" @click="addRole">新增角色</el-button>
            </el-row>
            <!-- table列表 -->
            <el-table border="" :data="list">
              <el-table-column align="center" type="index" label="序号" width="120" />
              <el-table-column align="center" prop="name" label="名称" width="240" />
              <el-table-column align="center" prop="description" label="描述" />
              <el-table-column align="center" label="操作" width="300">
                <template v-slot="{row}">
                  <el-button type="success" size="small" @click="AllotAuthor(row)">分配权限</el-button>
                  <el-button type="primary" size="small" @click="editRole(row)">编辑</el-button>
                  <el-button type="danger" size="small" @click="deleteRole(row)">删除</el-button>
                </template>
              </el-table-column>

            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <el-pagination
                layout="prev, pager, next"
                :total="page.total"
                :current-page="page.page"
                :page-size="page.pagesize"
                @current-change="changeCurrent"
              />
            </el-row>
          </el-tab-pane>
          <!-- 右 -->
          <el-tab-pane label="公司信息">
            <el-row>
              <el-alert
                title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
                type="info"
                :closable="false"
                show-icon
              />
            </el-row>
            <el-form :model="formData" label-width="120px" style="margin-top:50px">
              <el-form-item label="企业名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增角色对话框 -->
    <add-role v-if="showAddDialog" :show-dialog.sync="showAddDialog" @addSuccess="getRoleList" />
    <edit-role v-if="showEditDialog" :show-dialog.sync="showEditDialog" :edit-id="editRoleId" @editSuccess="getRoleList" />
    <allot-authority v-if="showAllotDialog" :show-dialog.sync="showAllotDialog" :role-id="roleId" />
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole } from '@/api/setting'
import AddRole from './components/add-role.vue'
import EditRole from './components/edit-role.vue'
import AllotAuthority from './components/allot-authority.vue'
import { mapGetters } from 'vuex'

export default {
  name: 'Setting',
  components: {
    AddRole,
    EditRole,
    AllotAuthority
  },
  data() {
    return {
      roleId: '',
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10,
        total: 0
      },
      list: [], // 角色列表
      showAddDialog: false, // 新增角色对话框的显示与隐藏
      showAllotDialog: false, // 分配权限对话框的显示与隐藏
      showEditDialog: false, // 编辑角色对话框的显示与隐藏
      formData: {}, // 公司信息
      editRoleId: ''// 编辑传递信息
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    // 分页当前页
    changeCurrent(newValue) {
      this.page.page = newValue
      this.getRoleList()
    },
    // 获取公司详情
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
      // console.log(this.formData)
    },
    // 删除
    async deleteRole(row) {
      try {
        await this.$confirm(`您确认删除 ${row.name} 吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        // 只有点击了确定 才能进入到下方
        await deleteRole(row.id) // 调用删除接口
        this.getRoleList() // 重新加载数据
        this.$message.success('删除角色成功!')
      } catch (err) {
        this.$message('删除角色失败!')
      }
    },
    // 新增角色
    addRole() {
      this.showAddDialog = true
    },
    // 编辑角色
    editRole(row) {
      this.editRoleId = row.id
      // console.log(row)
      this.showEditDialog = true
    },
    // 分配权限
    AllotAuthor(row) {
      this.roleId = row.id
      this.showAllotDialog = true
    }
  }
}
</script>

<style>

</style>
